<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_logo">
        <img src="@/assets/logo.png" alt="">
      </div>
      <!-- 登录表单 -->
      <el-form class="login_form" :model="form" :rules="loginFormRules" ref="loginForm">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="login_btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import $http from '@/utils/resquest.js'
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        // 用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        // 密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录
    login() {
      // 利用element UI 中validate的方法 配合$refs
      this.$refs.loginForm.validate(async e => {
        if (!e) return
        const { data: res } = await $http.post('login', this.form)
        console.log(res)
        // 登录失败
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        // 登录成功
        this.$message.success('登录成功')
        // 存储token
        sessionStorage.setItem('token', res.data.token)
        // 跳转页面
        this.$router.push('/home')
      })
    },
    // 重置表单信息
    resetLoginForm() {
      // 利用element UI 中resetFields的方法 配合$refs
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    // 盒子位于正中间
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .login_logo {
      position: absolute;
      left: 175px;
      top: -50px;
      width: 100px;
      height: 100px;
      border-radius: 50%;
      padding: 5px;
      box-sizing: border-box;
      background-color: #fff;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_form {
      padding: 0 15px;
      box-sizing: border-box;
      margin-top: 100px;
      .login_btn {
        display: flex;
        // 靠右边对齐
        justify-content: flex-end;
        // justify-content: flex-start;
      }
    }
  }
}
</style>